import { wxShare } from "@/utils";
import uuidv1 from "uuid/v1";
import { gbs } from "@/config/index";
export default {
  name: "",
  data() {
    return {
      req: {},
      login: false
    };
  },
  methods: {
    toSelect() {
      if (this.login) {
        this.$router.push("/select");
      } else {
        this.$vux.confirm.show({
          title: "提示",
          content: "请进行微信授权",
          onShow() {
            console.log("plugin show");
          },
          onHide() {
            console.log("plugin hide");
          },
          onCancel() {
            console.log("plugin cancel");
          },
          onConfirm() {
            let url = encodeURIComponent(window.location.href);
            window.location.href = `${gbs.wxUrl}?appid=${
              gbs.wxAppId
            }&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=1@connect_redirect=1#wechat_redirect`;
          }
        });
      }
    },
    question() {
      this.$$api_global_question({
        data: JSON.parse(window.localStorage.getItem("reqData")),
        succFn: res => {}
      });
      window.localStorage.setItem("reqData", JSON.stringify(this.req));
    },
    getRequest() {
      var url = location.href.split("?")[1]; //获取url中"?"符后的字串
      if (window.location.href.indexOf("?") == -1) return {};

      var theRequest = new Object();
      if (url.indexOf("=") != -1) {
        var strs = url.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
      }
      return theRequest;
    },
    isLogin(cb) {
      let param = this.getRequest();
      this.$$api_global_oauth({
        data: {
          code: param.code
        },
        succFn: res => {
          this.req.openid = res.openid;
          if (res.unionid) this.req.unionid = res.unionid;
          this.req.openid = res.openid;
          this.$vux.toast.text("授权成功！", "top");
          window.localStorage.setItem("user", JSON.stringify(res));
          window.localStorage.setItem("reqData", JSON.stringify(this.req));
          cb();
        }
      });
    },
    shareHandle(){
      this.$$api_global_question({
          data: {
              ...this.req,
              transmit_flag: 1
          },
          noLoading: true
      })
    }
  },
  mounted() {
    this.req = {
      user_flag: uuidv1(),
      ip_flag: "192.168.0.1",
      physical_flag: navigator.appVersion,
      record_type: "1",
      record_type_name: document.title,
      total_num_flag: 10,
      source_type: this.$route.query.source_type || ""
    };
    const params = this.getRequest();
    if (params.code) {
      this.isLogin(() => {
        this.login = true;
        this.question();
      });
    } else {
      if (!window.localStorage.getItem("user")) {
        this.login = false;
      } else {
        this.login = true;
        this.question();
      }
    }

    // 检测之前有没有授权过
    if(!window.localStorage.getItem("user") && !params.code){
        this.$vux.confirm.show({
          title: "提示",
          content: "请进行微信授权",
          onShow() {
            console.log("plugin show");
          },
          onHide() {
            console.log("plugin hide");
          },
          onCancel() {
            console.log("plugin cancel");
          },
          onConfirm() {
            let url = encodeURIComponent(window.location.href);
            window.location.href = `${gbs.wxUrl}?appid=${
              gbs.wxAppId
            }&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=1@connect_redirect=1#wechat_redirect`;
          }
        });
    }
    wxShare({
      title: "这些常用药99%的人都用错了，测一测你都用对了么？",
      text: "", //`10道常用药安全测试题，99%的人都答错，无知很可怕，测一测你的用药安全意识`,
      href: "http://app.allclinic.cn/h5/wx/startActivity/#/?source_type=3",
      imgUrl: "https://app.allclinic.cn/h5/wx/scan_drug/static/img/logo.jpg",
      appMessageSuccFn: () => {
        this.shareHandle()
      },
      timeLineSuccFn: () => {
        this.shareHandle()
      }
    });
  }
};